package com.zhuss.vent.vent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.vent.vent.DetailVentBo;
import com.zhuss.bean.bo.vent.vent.SearchVentBo;
import com.zhuss.bean.bo.vent.vent.wechat.ListVentBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.vent.vent.DetailVentVo;
import com.zhuss.bean.vo.vent.vent.ListVentVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.vent.processor.VentAppendRead;
import com.zhuss.vent.processor.VentRead;
import com.zhuss.vent.processor.VentTopicRead;


/**
 * vent查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("ventReadService")
public class VentReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("ventRead")
	private VentRead ventRead;
	
	@Autowired
	@Qualifier("ventTopicRead")
	private VentTopicRead ventTopicRead;
	
	@Autowired
	@Qualifier("ventAppendRead")
	private VentAppendRead ventAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailVentBo bo = (DetailVentBo) param;
		Long ventId = bo.getVentId();
		DetailVentVo vo = (DetailVentVo) ventRead.detail(ventId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchVentBo bo = (SearchVentBo) param;
		Object result = ventRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailVentBo param) throws WpsException {
		Long ventId = param.getVentId();
		Object result = ventTopicRead.listTopicTree(ventId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listVent(Object param) throws WpsException {
		ListVentBo bo = (ListVentBo) param;
		Object result = ventRead.listVent(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailVentVo> pageVo = (PageVo<DetailVentVo>) result;
		for (DetailVentVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = ventAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getVentId(), null).size();
			Integer collectNum = ventAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getVentId(), null).size();
			Integer forwardNum = ventAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getVentId(), null).size();
			Integer topicNum = ventTopicRead.listTopic(vo.getVentId(), 0l).size();
			
			Integer thumbSelfNum = ventAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getVentId(), userId).size();
			Integer collectSelfNum = ventAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getVentId(), userId).size();
			Integer forwardSelfNum = ventAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getVentId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListVentVo> pageVo = (PageVo<ListVentVo>) result;
		for (ListVentVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = ventAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getVentId(), null).size();
			Integer collectNum = ventAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getVentId(), null).size();
			Integer forwardNum = ventAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getVentId(), null).size();
			Integer topicNum = ventTopicRead.listTopic(vo.getVentId(), 0l).size();
			
			Integer thumbSelfNum = ventAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getVentId(), userId).size();
			Integer collectSelfNum = ventAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getVentId(), userId).size();
			Integer forwardSelfNum = ventAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getVentId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

